package com.coffeeloversupportlibrary.coffeelover.mercury.my;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Shop;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/28 9:55
 * @projectName CoffeeLoverSupportLibrary
 * @className AddressActivity.java
 * @description TODO
 */
public class AddressActivity extends AppCompatActivity {
    private SharedPreferences userInfo;
    private Activity activity = AddressActivity.this;
    private User user;
    private Shop.DataDTO.ListDTO goods;
    private int coffeeBeans;

    private ImageView iv_back, iv_img;
    private TextView tv_usermsg, tv_num, tv_goodname;
    private AppCompatButton btn_trueorder;

    private HttpUtils httpUtils = new HttpUtils();
    private JsonUtils jsonUtils = new JsonUtils();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_trueorder);

        initView();
        setView();
        setListener();
    }

    void setView(){
        tv_usermsg.setText(user.getData().getUser().getUserNickname()+"   "+user.getData().getUser().getUserTel());
        tv_num.setText(goods.getMallBeans()+"");
        tv_goodname.setText(goods.getMallGoods());
        Glide.with(activity).load(goods.getGoodsImg()).into(iv_img);
    }

    void setListener(){
        OrderOnClickListener listener = new OrderOnClickListener();
        btn_trueorder.setOnClickListener(listener);
        iv_back.setOnClickListener(listener);
    }

    void initView(){
        userInfo = getSharedPreferences("user", 0);
        user = jsonUtils.getUser(userInfo.getString("userInfo", ""));
        goods = (Shop.DataDTO.ListDTO) getIntent().getSerializableExtra("shopInfo");
        coffeeBeans = userInfo.getInt("coffeeBean", 0);

        tv_num = findViewById(R.id.tv_nummt);
        tv_goodname = findViewById(R.id.tv_brandmt);
        tv_usermsg = findViewById(R.id.tv_namephone);
        btn_trueorder = findViewById(R.id.btn_truemt);
        iv_img = findViewById(R.id.iv_imgmt);
        iv_back = findViewById(R.id.iv_back);
    }

    class OrderOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_truemt:
                    String url = "/user/center-mall/mallItem/" + user.getData().getUser().getUserId() + "/" + goods.getId();
                    Log.e("ShopActivity", "myOnClickURL: "+ url);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), "");
                    Call call = httpUtils.getCall(requestBody, url);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                            String res = response.body().string();
                            Log.e("CenterActivity","ShopReturn" + res);
                            Shop shop = jsonUtils.getShop(res);
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(shop.getMessage() == "兑换成功" || shop.getMessage().equals("兑换成功")){
                                        int changedCoffeeBeans = coffeeBeans - goods.getMallBeans();
                                        SharedPreferences.Editor editor = userInfo.edit();
                                        editor.putInt("coffeeBean", changedCoffeeBeans);
                                        editor.commit();
//                                tv_score.setText(changedCoffeeBeans+" 个");
                                        Intent intent = new Intent();
                                        intent.setAction("shopCoffeeBean");
                                        sendBroadcast(intent);
                                    }else{
                                        Toast.makeText(activity, "兑换失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    });
                    finish();
                    break;
                case R.id.iv_back:
                    finish();
                    break;
            }
        }
    }
}
